package com.wn.atm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 蜗牛ATM
 * 2019.8.6,数据存入文本，开机读取
 * @author song
 *
 */
public class WoniuATM
{

	private List<User> users = new ArrayList<User>();// 存储用户
	Scanner scanner = new Scanner(System.in);// 输入
	User loginUser;// 登陆用户
	final String PATH = "E:\\JavaWN\\Project1\\User.txt";// 用户储存路径
	//用户名密码两个空格隔开，密码money三个空格
	final String userNameToPassword="  ";
	final String passwordToMoney="   ";
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		WoniuATM atm = new WoniuATM();
		atm.mainMenu();
	}

	// 主菜单
	public void mainMenu()
	{
		initUser();
		System.out.println("==============欢迎使用蜗牛ATM===============");
		System.out.println("===请输入你的操作，1：登陆  2：注册  3：退出 ===");
		System.out.println("============================================");

		int option = scanner.nextInt();
		switch (option)
		{
		case 1:
			login();
			break;
		case 2:
			signIn();
			break;
		case 3:
			System.out.println("再见！");
			scanner.close();
			System.exit(0);
			break;
		default:
			break;
		}

	}

	/**
	 * 注册
	 */
	public void signIn()
	{
		// TODO Auto-generated method stub
		User user = new User();
		// 重复用户名则重新输入
		while (true)
		{
			System.out.println("请输入你的账户");
			user.setName(scanner.next());
			if (users.stream().filter(t -> t.getName().equals(user.getName())).count() <= 0)
			{
				// 输入密码
				while (true)
				{
					System.out.println("请输入你的密码");
					String password = scanner.next();
					System.out.println("请再次输入你的密码");
					String confirmPassword = scanner.next();
					if (confirmPassword.equals(password))
					{
						user.setPassword(password);
						break;
					} else
					{
						System.out.println("两次密码输入不一致！");
					}
				}
				users.add(user);
				insertUserToTxt(user);// 写入文件
				loginUser = user;// 登陆
				System.out.println("注册成功！");
				subMenu();// 進入子菜單
				break;//
			} else
			{
				System.out.println("用户名已占用!");
			}

		}

	}

	/**
	 * 登陆
	 */
	public void login()
	{
		// TODO Auto-generated method stub
		while (true)
		{
			System.out.println("请输入你的用户名");
			String name = scanner.next();
			////////
			/// lambda,stream() goooooood
			//////////////
			// 是否存在
			User user = new User();
			try
			{

				user = users.stream().filter(t -> t.getName().equals(name)).findFirst().get();
			} catch (Exception e)
			{
				user = null;
			}
			if (user != null)
			{

				System.out.println("请输入密码");
				String password = scanner.next();
				// 判断密码
				if (user.checkPassword(password))
				{
					loginUser = user;
					System.out.println("登陆成功！");
					subMenu();
					break;
				} else
				{
					System.out.println("密码错误");
				}

			} else
			{
				System.out.println("账户不存在，请先注册");
				signIn();
			}
		}
	}

	/**
	 * 将用户存入文件
	 * 
	 * @param users 传入的用户,多个
	 */
	public void insertUserToTxt(User... users)
	{
		File file = new File(PATH);// 打开文件夹
		FileOutputStream fStream = null;
		// 创建文件
		try
		{
			if (!file.exists())
			{
				file.createNewFile();
				fStream = new FileOutputStream(file);
			} else
			{
				// 这里构造方法多了一个参数true,表示在文件末尾追加写入
				fStream = new FileOutputStream(file, true);
			}
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}

		try
		{
			// 指定以UTF-8格式写入文件
			OutputStreamWriter osw = new OutputStreamWriter(fStream, "UTF-8");
			// 用户名密码两个空格隔开，密码money三个空格
			for (User user : users)
			{
				String userInfo = user.getName() + userNameToPassword + user.getPassword()+passwordToMoney+user.getMoney();
				osw.write(userInfo + "\n");
			}
			osw.close();
			fStream.close();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 从文件导入账户
	 */
	public void initUser()
	{
		File file=new File(PATH);
		InputStream is=null;
		Reader reader=null;
		BufferedReader bufferedReader=null;
		
		// 存在读取
		if(file.exists())
		{
			try
			{
				is=new FileInputStream(file);
				reader=new InputStreamReader(is);
				bufferedReader=new BufferedReader(reader);
				String line=null;
				while ((line=bufferedReader.readLine())!=null)
				{
					// 不包括end位置
					String name=line.substring(0,line.indexOf(userNameToPassword));
					String password=line.substring(line.indexOf(userNameToPassword)+userNameToPassword.length(),line.indexOf(passwordToMoney));
					double money=Double.parseDouble(line.substring(line.indexOf(passwordToMoney)+passwordToMoney.length()));
					users.add(new User(name,password,money));
					
				}
				reader.close();
				bufferedReader.close();
			} catch (Exception e)
			{
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	/**
	 * 登陆后子菜单
	 */
	public void subMenu()
	{
		// TODO Auto-generated method stub
		System.out.println("============================================");
		System.out.println("=============進入菜單，请选择你的操作=========");
		System.out.println("===========1.余额查询  2.存款  3.取款  4.转账  5.退出登陆=======");
		System.out.println("============================================");

		// 操作
		int option = scanner.nextInt();
		switch (option)
		{
		case 1:
			checkMoney();// 查看余额
			break;
		case 2:
			deposit();// 存款
			break;
		case 3:
			withdrawMoney();// 取款
			break;
		case 4:
			transferAccounts();// 转账
			break;
		case 5:
			logout();// 退出
			break;
		default:
			break;
		}
	}

	/**
	 * 取款
	 */
	public void withdrawMoney()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 存款
	 */
	public void deposit()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 注销登陆
	 */
	private void logout()
	{
		// TODO Auto-generated method stub
		loginUser = null;
		mainMenu();

	}

	/**
	 * 转账
	 */
	public void transferAccounts()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 查看余额
	 */
	public void checkMoney()
	{
		// TODO Auto-generated method stub
		System.out.println(loginUser.getMoney());
	}

}
